<template>
  <div class="image-gallery">
    <div class="grid-container">
      <!-- 改进1: 使用url作为唯一key -->
      <div v-for="(img, index) in images" :key="img.url" class="grid-item">
        <div class="image-wrapper">
          <!-- 改进2: 增加加载状态层 -->
          <div v-if="!loadedUrls.has(img.url)" class="loading-overlay">
            <div class="loader"></div>
          </div>

          <!-- 改进3: 增加错误提示 -->
          <div v-if="errorUrls.has(img.url)" class="error-overlay">图片加载失败</div>
          <!-- 改进4: 可访问性优化 -->
          <img
            :src="img.url"
            :alt="img.alt || '图片内容'"
            :style="{ 'aspect-ratio': img.aspectRatio }"
            @load="handleImageLoad(index)"
            @error="handleImageError(index)"
          />

          <div v-if="img.caption" class="image-caption">{{ img.caption }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'

interface ImageItem {
  url: string
  alt?: string
  aspectRatio?: string
  caption?: string
}

const props = defineProps<{
  images: ImageItem[]
}>()

// 状态管理改进
const columnCount = ref(1)
const loadedUrls = ref(new Set<string>())
const errorUrls = ref(new Set<string>())

// 响应式列数计算
// 改进5: 修复列数计算逻辑
// 响应式布局修正
const calculateColumns = () => {
  const width = window.innerWidth
  columnCount.value = width >= 900 ? 3 : width >= 600 ? 2 : 1
}

// 图片处理改进
const handleImageLoad = (index: number) => {
  loadedUrls.value.add(props.images[index].url)
}

const handleImageError = (index: number) => {
  errorUrls.value.add(props.images[index].url)
}

// 性能优化
const resizeTimer = ref<number | null>(null)
const debouncedCalculate = () => {
  if (resizeTimer.value) clearTimeout(resizeTimer.value)
  resizeTimer.value = window.setTimeout(calculateColumns, 200)
}

// 生命周期
onMounted(() => {
  calculateColumns()
  window.addEventListener('resize', debouncedCalculate)
})

onUnmounted(() => {
  window.removeEventListener('resize', debouncedCalculate)
  if (resizeTimer.value) clearTimeout(resizeTimer.value)
})
</script>

<style scoped>
.image-gallery {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.grid-item {
  display: flex;
  flex-direction: column;
}

.image-wrapper {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
  background: #f5f5f5;
  aspect-ratio: 1/1;
  /* 默认宽高比 */
}

.image-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.3s ease;
}

.loading-overlay,
.error-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
}

.error-overlay {
  color: #ff4d4f;
  font-size: 1rem;
}

.loader {
  border: 3px solid #f3f3f3;
  border-top: 3px solid #1890ff;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.image-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 1rem;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
  color: white;
  font-size: 0.9rem;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

@media (max-width: 768px) {
  .grid-container {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 1rem;
  }

  .image-caption {
    font-size: 0.8rem;
    padding: 0.75rem;
  }
}

@media (max-width: 480px) {
  .grid-container {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .image-wrapper {
    aspect-ratio: 4/3;
  }
}
</style>
